import sys
from PHPSerialize import *
from PHPUnserialize import *
from WifiFinderService import *
from Address import *
import unicodedata

phps = PHPSerialize()
phpu = PHPUnserialize()

for line in sys.stdin:
    data = line

clientreq = phpu.unserialize(data)

if clientreq[0] == "getPosition":
    
    macs = []
    strenghts = []
    
    for key in clientreq[1][0]:
        macs.append(clientreq[1][0][key])
        strenghts.append(clientreq[1][1][key])
    #endfor
    try:
        coordsAndFloor = WifiFindService(macs, strenghts)
        coord_x = coordsAndFloor[2][0]
        coord_y = coordsAndFloor[2][1]
        buildingNumber = coordsAndFloor[3]
        floorNumber = coordsAndFloor[4]
        r1 = coordsAndFloor[5]
        r2 = coordsAndFloor[6]
        rcomb = coordsAndFloor[7]
    except ValueError:
        buildingNumber = -1
        floorNumber = 0
        coord_x = 0
        coord_y = 0
    clientout = (buildingNumber, floorNumber, int(coord_x), int(coord_y))
    
    print phps.serialize(clientout)

elif clientreq[0] == "getMapUrl":
    try:
        floor_id = clientreq[1];
        mapurl = Addresses().ReturnMapUrlFromFloorId(floor_id)
        #NORMALIZZARE DA UNICODE A STRINGA
    except ValueError:
        mapurl = ""
    print phps.serialize(unicodedata.normalize('NFKD', mapurl).encode('ascii','ignore'))

#endif